<template>
  <t-tabs v-model="tabValue">
    <t-tab-panel value="1" label="奖励信息">
      <t-form ref="form" class="step-form" labelWidth="120px" :colon="true">
        <t-row style="flex: 1">
          <t-col :span="12">
            <t-form-item label="奖励名称" name="name">
              {{ objData.name }}
            </t-form-item>
            <t-form-item label="奖项种类" name="awardsLevel">
              {{ objData.awardsType | filterByDict(dict.type.awards_type) }}
            </t-form-item>
            <t-form-item label="奖项级别" name="awardsLevel">
              {{ objData.awardsLevel | filterByDict(dict.type.awards_level) }}
            </t-form-item>
            <t-form-item label="获奖等级" name="awardsGrade">
              {{ objData.awardsGrade | filterByDict(dict.type.awards_grade) }}
            </t-form-item>
            <t-form-item label="获奖时间" name="awardsDate">
              {{ objData.awardsDate }}
            </t-form-item>
            <t-form-item label="颁奖单位" name="publishDept">
              {{ objData.publishDept }}
            </t-form-item>
            <t-form-item label="填报单位" name="declareDeptName">
              {{ objData.declareDeptName }}
            </t-form-item>
            <t-form-item label="关键词" name="keyWord">
              {{ objData.keyWord }}
            </t-form-item>
            <t-form-item label="备注" name="note">
              {{ objData.note }}
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
    </t-tab-panel>
    <t-tab-panel value="2" label="获奖人员">
      <awards-member :objId="objId" v-model="objData.memberList" :editOpen="false"></awards-member>
    </t-tab-panel>
    <t-tab-panel value="3" label="奖励项目来源">
      <awards-project :objId="objId" ref="awards-project" v-model="objData.projectList" :editOpen="false"
        style="height: 58vh;width: 100%; overflow: auto; overflow-x: hidden;"></awards-project>
    </t-tab-panel>
    <t-tab-panel value="5" label="附件信息">
      <t-table row-key="id" :columns="projectFileColumns" :data="projectFileData">
        <template #id="{ row, rowIndex }">
          {{ rowIndex + 1 }}
        </template>
        <template #c1="{ row }">
          <a v-if="row.c3" class="t-button-link" :href="`${file_domain}/${row.c3}`" target="_blank">{{ row.c1 }}</a>
          <div v-else>{{ row.c1 }}</div>
        </template>
        <template #op="{ row }">
          <div v-for="(item, index) in row.fileLists" :key="index">
            <a class="t-button-link" target="_blank" :href="item.url">
              {{ item.name }}
            </a>
          </div>
        </template>
      </t-table>
    </t-tab-panel>
  </t-tabs>
</template>

<script>
import { VITE_FILE_DOMAIN } from '@/api/upload'
import { getFiles } from "@/api/common";
import awardsMember from "./awards-member.vue";
import awardsProject from "./awards-project.vue";

export default {
  name: "awards-detail",
  dicts: ['awards_level', 'awards_grade', 'awards_type'],
  components: {
    awardsMember,
    awardsProject
  },
  props: {
    objId: [String, Number],
  },
  data () {
    return {
      site_tag: import.meta.env.VITE_SITE_TAG,
      file_domain: VITE_FILE_DOMAIN,
      tabValue: '1',
      objData: {},
      projectFileData: [
        {
          id: 1,
          c1: "获奖证书",
          c2: "是",
          moduleKey: "awardsZS",
          fileIds: [],
          fileLists: [],
          require: true,
        },
        {
          id: 4,
          c1: "其他",
          c2: "否",
          fileIds: [],
          fileLists: [],
          moduleKey: "awardsqt",
          require: false
        },
      ],
      projectFileColumns: [
        {
          colKey: "id",
          title: "序号",
          align: "center",
          width: 64
        },
        {
          colKey: "c1",
          title: "附件名称",
          align: "center",
        },
        {
          colKey: "c2",
          title: "是否必需",
          align: "center",
        },
        {
          colKey: "op",
          title: "附件列表",
          align: "left",
        },
      ],
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler (val) {
        if (val) {
          this.getObj(val);
        }
      }
    },
  },
  methods: {
    getObj () {
      // 获取项目附件
      getFiles(this.objId).then((res) => {
        const files = {}
        res.data.forEach((item) => {
          if (!files[item.moduleKey]) {
            files[item.moduleKey] = []
          }
          files[item.moduleKey].push({
            name: item.fileName,
            url: item.fullFilePath,
            id: item.id,
            filePath: item.filePath
          })
        })
        this.projectFileData.forEach((val, index) => {
          this.projectFileData[index].fileLists = files[val.moduleKey] || [];
          this.projectFileData[index].fileIds = files[val.moduleKey]?.map(val => val.id) || [];
        })

      }).catch((e) => {
        this.$message.error(e.toString());
      });
      this.$api.cgzh.awards.get(this.objId).then(response => {
        this.objData = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    }
  }
}
</script>

<style lang="less" scoped>
.t-tab-panel {
  padding: 10px;
}

.t-form__item {
  margin-bottom: 6px
}
</style>
